import os
from google.appengine.ext.webapp import template
import cgi
import Cookie

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class User(db.Model):
  name = db.StringProperty(multiline=False)
  password = db.StringProperty(multiline=False)

class MainPage(webapp.RequestHandler):
  def get(self):
  
    template_values = {
      'data': 'null'
    }

#    e = User(name="xiad", password="cad")
#    e.put()
    
    if (not Login().islogin(self.response)):
      self.response.out.write(template.render(os.path.join(os.path.dirname(__file__), 'login.html'), template_values))
      return

    sep = os.sep
    left = os.path.dirname(__file__)
    right = self.request.path.replace('/', sep).lstrip(sep)
    path = os.path.join(left, right)
    
    if (len(right) == 0):
      path = os.path.join(path, 'Default.html')
    elif right == 'photo':
      path = os.path.join(path, 'photo.html')
    elif right == 'about':
      path = os.path.join(path, 'about.html')
    elif right == 'draft':
      path = os.path.join(path, 'moo.html')
    elif (right.find('case') != -1):# == 'case\\isaved':
      path = path + '.html'
#    else:
#      path = left + right + '.html'
      
    self.response.out.write(template.render(path, template_values))

'''
    self.response.out.write(left)
    self.response.out.write('<br/>')
    self.response.out.write(right)
    self.response.out.write('<br/>')
    self.response.out.write(path)
    self.response.out.write('<br/>')
#    self.response.out.write(sep)
#    self.response.out.write('<br/>')
'''

class Error(webapp.RequestHandler):
  def get(self):
    self.response.out.write(template.render(os.path.join(os.path.dirname(__file__), 'error.html'), ''))

class Login(webapp.RequestHandler):
  def post(self):
    C = Cookie.SimpleCookie()
    C['name'] = self.request.get('name')
    C['password'] = self.request.get('password')
    print C.output()
    self.redirect('/')
    
#    if (C['name'].value == 'xiad' and C['password'].value == 'xiad'):
#      print C.output()
#      self.response.out.write(C)
#      self.redirect('/')
#    else:
#      self.response.out.write(template.render(os.path.join(os.path.dirname(__file__), 'login.html'), ''))

  def islogin(self, response):
    C = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE"))
#    C['name'] = 'x'
#    print C.output()
#    response.out.write(C)
    
    name = C.get('name')
    psd = C.get('password')
    if (name is None or psd is None):
      return False
    else:
#      C.get('name').value != 'xiad'
      users = db.GqlQuery("select * from User")# where name = '" + name + "' and password = '" + psd + "'")
      for user in users:
        if (user.name == name.value and user.password == psd.value):
          return True
      
      return False
      
    return False
    
      
      
    
	
application = webapp.WSGIApplication(
                                     [('/sign', Login),
                                      ('/', MainPage),
                                      ('/photo', MainPage),
                                      ('/draft', MainPage),
                                      ('/about', MainPage),
                                      ('/case/isaved', MainPage),
                                      ('/case/footprint', MainPage),
                                      ('/case/esbjerg', MainPage),
                                      ('/case/wedo', MainPage),
                                      ('/case/eps', MainPage),
                                      ('/case/machinery', MainPage),
                                      ('/.*', Error)
                                     ],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
